package com.leishixiong.dao;

import com.leishixiong.blog.model.Article;
import com.leishixiong.util.DBUtil;

import java.sql.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class ArticleDao {
    // TODO: 也没有考虑过分页的问题
    public List<Article> selectListByUid(int uid) {
        List<Article> articleList = new ArrayList<>();
        String sql = "SELECT aid, title, type, content, published_at FROM articles WHERE uid = ? ORDER BY published_at DESC";
        try (Connection c = DBUtil.connection()) {
            try (PreparedStatement ps = c.prepareStatement(sql)) {
                ps.setInt(1, uid);
                System.out.println("DEBUG: 执行SQL: " + ps);
                try (ResultSet rs = ps.executeQuery()) {
                    while (rs.next()) {
                        int aid = rs.getInt("aid");
                        String title = rs.getString("title");
                        String type = rs.getString("type");
                        String content = rs.getString("content");
                        // 需要从 java.sql.Date 转成 java.time.LocalDate 类型
                        LocalDate publishedAt = rs.getDate("published_at").toLocalDate();

                        Article article = new Article(aid, uid, title, type, content, publishedAt);

                        articleList.add(article);
                    }
                }
            }
        } catch (SQLException exc) {
            throw new RuntimeException(exc);
        }

        return articleList;
    }

    public int selectArticleCountByUid(int uid) {
        String sql = "SELECT COUNT(*) FROM articles WHERE uid = ?";
        try (Connection c = DBUtil.connection()) {
            try (PreparedStatement ps = c.prepareStatement(sql)) {
                ps.setInt(1, uid);
                System.out.println("DEBUG: 执行SQL: " + ps);
                try (ResultSet rs = ps.executeQuery()) {
                    rs.next();

                    return rs.getInt(1);
                }
            }
        } catch (SQLException exc) {
            throw new RuntimeException(exc);
        }
    }

    public int selectTypeCountByUid(int uid) {
        String sql = "SELECT COUNT(DISTINCT type) FROM articles WHERE uid = ?";
        try (Connection c = DBUtil.connection()) {
            try (PreparedStatement ps = c.prepareStatement(sql)) {
                ps.setInt(1, uid);
                System.out.println("DEBUG: 执行SQL: " + ps);
                try (ResultSet rs = ps.executeQuery()) {
                    rs.next();

                    return rs.getInt(1);
                }
            }
        } catch (SQLException exc) {
            throw new RuntimeException(exc);
        }
    }

    // aid 是不是当前用户发表的
    public Article selectOneByAid(int aid) {
        String sql = "SELECT uid, title, type, content, published_at FROM articles WHERE aid = ?";
        try (Connection c = DBUtil.connection()) {
            try (PreparedStatement ps = c.prepareStatement(sql)) {
                ps.setInt(1, aid);
                System.out.println("DEBUG: 执行SQL: " + ps);
                try (ResultSet rs = ps.executeQuery()) {
                    if (!rs.next()) {
                        return null;
                    }

                    int uid = rs.getInt("uid");
                    String title = rs.getString("title");
                    String type = rs.getString("type");
                    String content = rs.getString("content");
                    // 需要从 java.sql.Date 转成 java.time.LocalDate 类型
                    LocalDate publishedAt = rs.getDate("published_at").toLocalDate();

                    return new Article(aid, uid, title, type, content, publishedAt);
                }
            }
        } catch (SQLException exc) {
            throw new RuntimeException(exc);
        }
    }

    public void insert(Article article) {
        String sql = "INSERT INTO articles (uid, title, type, content, published_at) VALUES (?, ?, ?, ?, ?)";
        try (Connection c = DBUtil.connection()) {
            try (PreparedStatement ps = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
                ps.setInt(1, article.uid);
                ps.setString(2, article.title);
                ps.setString(3, article.type);
                ps.setString(4, article.content);
                // 从 java.time.LocalDate -> java.sql.Date
                ps.setDate(5, Date.valueOf(article.publishedAt));
                System.out.println("DEBUG: 执行SQL: " + ps);
                ps.executeUpdate();

                try (ResultSet rs = ps.getGeneratedKeys()) {
                    rs.next();
                    article.aid = rs.getInt(1);
                }
            }
        } catch (SQLException exc) {
            throw new RuntimeException(exc);
        }
    }
}
